package com.eeepay.v2_library.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.eeepay.v2_library.R;


/**
 * Created by zw on 2016/3/14 0014.
 * 一左一右
 */
public class LeftRightText extends RelativeLayout {

    private View view;
    private String leftText, rightText;
    private TextView tv_left, tv_right, tv_rightLeft;
    private int rightTextColor, backgroundColor, leftTextColor;
    private boolean toRight;//是否居右
    private float rightTextSize;
    private final float defaultRtSize = 15;
    private boolean singleLine = false;

    public LeftRightText(Context context) {
        this(context, null);
    }

    public LeftRightText(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.LeftRightText);
        leftText = typedArray.getString(R.styleable.LeftRightText_left_Text);
        rightText = typedArray.getString(R.styleable.LeftRightText_right_Text);
        rightTextSize = typedArray.getFloat(R.styleable.LeftRightText_right_text_size, defaultRtSize);
        rightTextColor = typedArray.getColor(R.styleable.LeftRightText_lrtRightColor, getResources().getColor(R.color.lrt_grey));
        leftTextColor = typedArray.getColor(R.styleable.LeftRightText_lrtLeftColor, getResources().getColor(R.color.lrt_textColor));
        rightTextColor = typedArray.getColor(R.styleable.LeftRightText_lrtRightColor, getResources().getColor(R.color.right_text_color));
        leftTextColor = typedArray.getColor(R.styleable.LeftRightText_lrtLeftColor, getResources().getColor(R.color.left_text_color));
        backgroundColor = typedArray.getColor(R.styleable.LeftRightText_background_Color, getResources().getColor(R.color.white));
        toRight = typedArray.getBoolean(R.styleable.LeftRightText_to_right, true);
        singleLine = typedArray.getBoolean(R.styleable.LeftRightText_singleLine, false);

        view = LayoutInflater.from(context).inflate(R.layout.view_left_right_text, this, true);

        tv_left = (TextView) view.findViewById(R.id.tv_left);
        tv_right = (TextView) view.findViewById(R.id.tv_right);
        tv_rightLeft = (TextView) view.findViewById(R.id.tv_right_left);

        setViewContent();
        typedArray.recycle();
    }

    private void setViewContent() {
        if (tv_left != null) {
            tv_left.setText(leftText);
        }
        if (tv_right != null) {
            tv_right.setText(rightText);
        }

        tv_left.setTextColor(leftTextColor);
        tv_right.setTextColor(rightTextColor);
        tv_right.setTextSize(rightTextSize);
        view.setBackgroundColor(backgroundColor);
        tv_right.setSingleLine(singleLine);

        tv_right.setGravity(toRight ? Gravity.RIGHT : Gravity.NO_GRAVITY);
    }

    /**
     * 设置右边的文字
     *
     * @param text
     */
    public void setRightText(CharSequence text) {
        tv_right.setText(text);
    }

    /**
     * @param text
     * @param color 设置右边文字及字体颜色
     */
    public void setRightTextColor(CharSequence text, int color) {
        tv_right.setText(text);
        tv_right.setTextColor(getResources().getColor(color));
    }

    /**
     * 获取右边文字
     *
     * @return
     */
    public String getRightText() {
        return tv_right.getText().toString();
    }

    /**
     * 设置右边文字的颜色
     *
     * @param color
     */
    public void setRightTextColor(int color) {
        tv_right.setTextColor(getResources().getColor(color));
    }

    /**
     * 设置左边的文字
     *
     * @param text
     */
    public void setLeftText(CharSequence text) {
        tv_left.setText(text);
    }

    public void setRightTextSize(int size){
        if(tv_right != null){
            tv_right.setTextSize(size);
        }
    }


    /**
     * 获取左边文字
     *
     * @return
     */
    public String getLeftText() {
        return tv_left.getText().toString();
    }

    /**
     * 设置左边文字的颜色
     *
     * @param color
     */
    public void setLeftTextColor(int color) {
        tv_left.setTextColor(getResources().getColor(color));
    }

    /**
     * @return
     */
    public String getRighText() {
        return tv_right.getText().toString().trim();
    }

    /**
     * 不调用 此控件不显示
     * @param text
     */
    public void setRightLeftText(String text){
        if(tv_rightLeft != null){
            tv_rightLeft.setVisibility(VISIBLE);
            tv_rightLeft.setText(text);
        }
    }

    /**
     * 获取右边的TextView
     * @return
     */
    public TextView getRTextView(){
        return tv_right;
    }

}